﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;

public class GlocalCache
{
    #region 公共使用
    /// <summary>
    /// 获取全局缓存数据
    /// </summary>
    /// <param name="KeyName">关键词</param>        
    public static DataTable GetApplicationData(string KeyName)
    {
        HttpApplicationState has = HttpContext.Current.Application;
        if (has[KeyName] != null) { return (DataTable)has[KeyName]; }
        return null;
    }
    /// <summary>
    /// 设置全局缓存数据
    /// </summary>
    /// <param name="KeyName">关键词</param>
    /// <param name="cdata">数据表</param>       
    public static void SetApplicationData(string KeyName, DataTable cdata)
    {
        HttpApplicationState has = HttpContext.Current.Application;
        has[KeyName] = cdata;
    }
    /// <summary>
    /// 移除全局缓存数据
    /// </summary>
    /// <param name="KeyName">关键词</param>        
    public static void RemoveApplicationData(string KeyName)
    {
        HttpApplicationState has = HttpContext.Current.Application;
        has.Remove(KeyName);
    }
    /// <summary>
    /// 获取缓存数据的下拉服务器控件
    /// </summary>        
    public static ListItem[] GetCache_ListItems(DataTable dt, string DataText, string DataValue)
    {
        List<ListItem> lis = new List<ListItem>();

        foreach (DataRow dr in dt.Rows)
        {
            lis.Add(new ListItem(Convert.ToString(dr[DataText]), Convert.ToString(dr[DataValue])));
        }

        return lis.ToArray();
    }
    /// <summary>
    /// 获取缓存数据的HTML控件
    /// </summary>        
    public static string GetCache_SelectOption(DataTable dt, string DataText, string DataValue, string SelectedValue)
    {
        StringBuilder sb = new StringBuilder();
        string val = string.Empty;
        foreach (DataRow dr in dt.Rows)
        {
            val = Convert.ToString(dr[DataValue]);
            sb.Append("<option ").Append((val == SelectedValue) ? "selected='selected'" : string.Empty).Append(" value='").Append(val).Append("'>").Append(dr[DataText]).Append("</option>");
        }
        return sb.ToString();
    }
    /// <summary>
    /// 获取枚举的键对值的匹配项 [返回] 没有:-1
    /// </summary>        
    public static string GetCache_match(DataTable dt, string DataText, string DataValue, string matchObj, bool IsValueToText)
    {
        string txt = string.Empty;
        string val = string.Empty;
        foreach (DataRow dr in dt.Rows)
        {
            txt = Convert.ToString(dr[DataText]);
            val = Convert.ToString(dr[DataValue]);
            if (IsValueToText)
            {
                if (val == matchObj) { return txt; }
            }
            else { if (txt == matchObj) { return val; } }
        }
        return string.Empty;
    }
    #endregion

    public static string Getbool_match(object matchObj)
    {
        string rs = string.Empty;

        if (matchObj.GetType() == typeof(string))
        {
            return (Convert.ToString(matchObj).ToLower() == "true") ? "是" : "否";
        }

        switch (Convert.ToInt32(matchObj))
        {
            case 0: rs = "否"; break;
            case 1: rs = "是"; break;
        }
        return rs;
    }

    public static string GetFileDown_match(object matchObj)
    {
        string rs = "无";
        string filepath = Convert.ToString(matchObj);

        if (filepath != string.Empty)
        {
            rs = "<a href='" + filepath + "' target='_blank'>[点击下载]</a>";
        }
        return rs;
    }

    public static string GetTrue_match(object matchObj, string trueShow)
    {
        string rs = string.Empty;

        switch (Convert.ToInt32(matchObj))
        {                
            case 1: rs = trueShow; break;
        }
        return rs;
    }

    public static string Getonline_match(object matchObj)
    {
        string rs = string.Empty;

        switch (Convert.ToInt32(matchObj))
        {
            case 0: rs = "离线"; break;
            case 1: rs = "在线"; break;
        }
        return rs;
    }

    public static string Getsex_match(object matchObj)
    {
        string rs = string.Empty;

        switch (Convert.ToInt32(matchObj))
        {
            case 0: rs = "女"; break;
            case 1: rs = "男"; break;
        }
        return rs;
    }

    /// <summary>
    /// 项目节点集
    /// </summary>        
    public static DataTable GetProjectNodes_Data()
    {
        DataTable dt = GetApplicationData("ProjectNodes");
        if (dt == null)
        {
            dt = DataEntity.tab_Class.SelectList("00", -1, false);
            SetApplicationData("ProjectNodes", dt);
        }
        return dt;
    }

    /// <summary>
    /// 获取项目节点集
    /// </summary>       
    /// <param name="ValBind">0.编号 1.结构 2.文本</param> 
    public static ListItem[] GetProjectNodes_ListItems(int ValBind)
    {
        return GetProjectNodes_ListItems(GetProjectNodes_Data(), ValBind);
    }

    /// <summary>
    /// 获取项目节点集
    /// </summary>
    /// <param name="dt">表</param>
    /// <param name="ValBind">0.编号 1.结构 2.文本</param>        
    public static ListItem[] GetProjectNodes_ListItems(DataTable dt, int ValBind)
    {            
        List<ListItem> lc = new List<ListItem>();
        int LevelNumber = 2;    //节点序列长度        

        if (dt == null || dt.Rows.Count == 0) { return null; }
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            ListItem li = new ListItem();

            int structid = Convert.ToString(dr["cls_StructId"]).Length;
            string relat = string.Empty;
            if (structid > LevelNumber)
            {
                relat = new string(' ', (structid / LevelNumber) * 5 - 8) + "└ ";
                relat = relat.Replace(" ", HttpUtility.HtmlDecode("&nbsp;"));
            }

            li.Text = relat + Convert.ToString(dr["cls_NodeText"]);

            string val = string.Empty;
            switch (ValBind)
            {
                case 0:
                    val = Convert.ToString(dr["cls_ID"]);
                    break;
                case 1:
                    val = Convert.ToString(dr["cls_StructId"]) + Convert.ToString(dr["cls_NodeValue"]);
                    break;
                case 2:
                    val = Convert.ToString(dr["cls_NodeText"]);
                    break;
            }

            li.Value = val;

            lc.Add(li);
        }
        return lc.ToArray();
    }

    public static string GetProjectNodes_match(object matchObj, bool isFullPath)
    {
        string matchStr = Convert.ToString(matchObj);
        DataTable dt = GetProjectNodes_Data();
        DataRow[] drs = null;
        if (isFullPath)
        {
            drs = dt.Select("(cls_StructId + cls_NodeValue) = '" + matchStr + "'");
        }
        else { drs = dt.Select("cls_ID = " + matchStr); }                
        
        if (drs != null && drs.Length != 0) { return Convert.ToString(drs[0]["cls_NodeText"]); }
        return string.Empty;
    }

    /// <summary>
    /// 获取数据源类别
    /// </summary>
    public static ListItem[] GetDataBaseType_ListItems()
    {
        List<ListItem> lis = new List<ListItem>();

        foreach (int temp in Enum.GetValues(typeof(DataBaseConnection.DataBaseType)))
        {
            DataBaseConnection.DataBaseType dbt = (DataBaseConnection.DataBaseType)temp;
            lis.Add(new ListItem(dbt.ToString(), temp.ToString()));
        }

        return lis.ToArray();
    }

    /// <summary>
    /// 绑定数据源类别
    /// </summary>
    /// <param name="matchObj">值</param>    
    public static string GetDataBaseType_match(object matchObj)
    {
        int temp = Convert.ToInt32(matchObj);
        try
        {
            return ((DataBaseConnection.DataBaseType)temp).ToString();
        }
        catch { }
        return string.Empty;
    }

    /// <summary>
    /// 获取版位列表
    /// </summary>        
    public static DataTable GetAdZone_Data()
    {
        DataTable dt = GetApplicationData("AdZone");
        if (dt == null)
        {
            dt = new DataEntity.tab_AdZone().SelectDataTable("1=1", null, null);
            SetApplicationData("AdZone", dt);
        }
        return dt;
    }

    /// <summary>
    /// 获取版位下拉列表
    /// </summary>
    public static ListItem[] GetAdZone_ListItems()
    {
        return GetCache_ListItems(GetAdZone_Data(), "adz_ZoneName", "adz_ID");
    }

    /// <summary>
    /// 获取版位匹配文本
    /// </summary>    
    public static string GetAdZone_match(object matchObj)
    {
        return GetCache_match(GetAdZone_Data(), "adz_ZoneName", "adz_ID", Convert.ToString(matchObj), true);
    }
}
